<template>
  <div>
      <!-- <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        <el-breadcrumb-item>用户列表</el-breadcrumb-item>
      </el-breadcrumb> -->
      <Breadcrmb title1="首页" title2="用户管理" title3="用户列表"></Breadcrmb>

        <el-card style="margin-top:20px">
            <el-row :gutter="20">
                <el-col :span="8">
                    <el-input placeholder="请输入内容" clearable v-model="queryInfo.query" @clear="getUser" >
                    <el-button slot="append" icon="el-icon-search" @click="getUser" ></el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="dialogVisible = true">添加</el-button>
                </el-col>
            </el-row>

             <el-table :data="tableData" style="width: 100%">
                <el-table-column prop="username" label="姓名">
                </el-table-column>
                <el-table-column prop="email" label="邮箱">
                </el-table-column>
                <el-table-column prop="mobile" label="电话">
                </el-table-column>
                <el-table-column prop="role_name" label="角色">
                </el-table-column>
                <el-table-column prop="mobile" label="状态">
                     <template slot-scope="scope">
                         <el-switch v-model="scope.row.mg_state" @change="userStateChanged(scope.row)"></el-switch>
                     </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                  <el-button size="mini" type="primary" icon="el-icon-edit" @click="toView(scope.row)"></el-button>
                  <el-button size="mini" type="danger" icon="el-icon-delete" @click='del(scope.row)'></el-button>
                  <el-tooltip effect="dark" content="分配角色" placement="top" :enterable="false"><el-button size="mini" type="warning" icon="el-icon-setting" @click='role(scope.row)'></el-button></el-tooltip>
                </template>
                </el-table-column>
             </el-table>

             <el-pagination @size-change="handleSizeChange" style="text-align: center;" :page-sizes="[2, 5, 10]" @current-change="handleCurrentChange" :page-size="queryInfo.pagesize" :current-page="queryInfo.pagenum" layout="total,sizes, prev, pager, next,jumper" :total="total">
            </el-pagination>

            <el-dialog title="添加用户" :before-close="handleClose" :visible.sync="dialogVisible" width="40%">
                <el-form :rules="rules" ref="addForm" label-width="80px" :model="userForm">

                    <el-form-item label="用户名" prop="username">
                        <el-input v-model="userForm.username"></el-input>
                    </el-form-item>

                    <el-form-item label="密码" prop="password">
                        <el-input v-model="userForm.password"></el-input>
                    </el-form-item>

                    <el-form-item label="邮箱" prop="email">
                        <el-input v-model="userForm.email"></el-input>
                    </el-form-item>

                    <el-form-item label="手机" prop="mobile">
                        <el-input v-model="userForm.mobile"></el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="handleClose">取 消</el-button>
                    <el-button type="primary" @click="addUser">确 定</el-button>
                </span>
            </el-dialog>

            <el-dialog title="编辑用户" :before-close="handleClose" :visible.sync="dialogVisible2" width="40%">
                <el-form :rules="rules" ref="rulesForm" label-width="80px" :model="userForm">

                    <el-form-item label="用户名" prop="username">
                        <el-input v-model="userForm.username"></el-input>
                    </el-form-item>

                    <el-form-item label="邮箱" prop="email">
                        <el-input v-model="userForm.email"></el-input>
                    </el-form-item>

                    <el-form-item label="手机" prop="mobile">
                        <el-input v-model="userForm.mobile"></el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="handleClose">取 消</el-button>
                    <el-button type="primary" @click="xgUser">确 定</el-button>
                </span>
            </el-dialog>
        </el-card>
  </div>
</template>

<script>
import {getUserList,StateChanged,getAddUser,getDeleteUser,getxgUser} from '@/api/user.js'

export default {
    created(){
       this.getUser();
    },
    methods:{
        //修改
        toView(data){
            console.log(data);
            this.userForm.email = data.email;
            this.userForm.id = data.id;
            this.userForm.username = data.username;
            this.userForm.mobile = data.mobile;
            this.dialogVisible2 = true;
        },
        async xgUser(){
            var {data:res}=await getxgUser(`users/${this.userForm.id}`,this.userForm);
            console.log(res);
            if(res.meta.status !==200){
                return this.$message.error("修改失败！")
            }
            this.$message.success("修改成功");
            this.getUser();
            this.handleClose();
        },
        //删除
        async del(row){
            // console.log(row.id);
            var {data:res}=await getDeleteUser(`users/${row.id}`);
            console.log(res);
            if(res.meta.status !==200){
                return this.$message.error("删除失败！")
            }
            this.$message.success("删除成功");
            this.getUser();
        },
        //添加
        async addUser(){
            var {data:res}=await getAddUser(this.userForm);
            console.log(res);
            if(res.meta.status !==201){
                return this.$message.error("添加失败！")
            }
            this.$message.success("更新用户添加成功");
            this.handleClose();
            this.getUser();
        },
        handleSizeChange(size) {
            this.queryInfo.pagesize = size;
            this.getUser();
        },
        handleCurrentChange(num) {
            this.queryInfo.pagenum = num;
            this.getUser();
        },
        handleClose() {
            // this.$refs['rulesForm'].resetFields();
            this.userForm.email = '';
            this.userForm.username = '';
            this.userForm.mobile = '';
            this.userForm.password = '';
            this.dialogVisible = false;
            this.dialogVisible2 = false;
        },
        async getUser(){
             var {data:res}=await getUserList(this.queryInfo);
            console.log(res);
            if(res.meta.status !==200){
                return this.$message.error("获取用户列表失败！")
            }
            this.tableData=res.data.users;
            this.total=res.data.total;
        },
        async userStateChanged(userinfo){
            // const {data:res} =await this.request.put(
            //     `users/${userinfo.id}/state/${userinfo.mg_state}`
            // );
            const {data:res} =await StateChanged(`users/${userinfo.id}/state/${userinfo.mg_state}`);
            if(res.meta.status !==200){
                userinfo.mg_state=!userinfo.mg_state;
                return this.$message.error("更新用户状态失败");
            }
            this.$message.success("更新用户状态成功");
        }
    },
    data(){ 
        var validateMobile = (rule, value, callback) => {
            const regEmail = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;

            if (regEmail.test(value)) {
                return callback();
            }
            callback(new Error("请输入合法的手机号"));
        };
        var validateEmail = (rule, value, callback) => {
            const regEmail = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;

            if (regEmail.test(value)) {
                return callback();
            }
            callback(new Error("请输入合法的邮箱"));
        }
        return {
            rules: {
                username: [{
                    required: true,
                    message: '请输入用户名',
                    trigger: 'blur'
                }],
                password: [{
                    required: true,
                    message: '请输入密码',
                    trigger: 'blur'
                }],
                email: [{
                    required: true,
                    message: '请输入邮箱',
                    trigger: 'blur'
                }, {
                    validator: validateEmail,
                    trigger: 'blur'
                }],
                mobile: [{
                    required: true,
                    message: '请输入电话号码',
                    trigger: 'bler'
                }, {
                    validator: validateMobile,
                    trigger: 'blur'
                }]
            },
            searchInput:'',
            tableData:[],
            queryInfo:{
                pagesize:2,
                pagenum:1,
                query:''
            },
            // userlist:'',
            total: 0, //总记录数
            dialogVisible2:false,
            dialogVisible: false,
            userForm:{
                username:'',
                password:'',
                email:'',
                mobile:''
            }
        }
    }
}
</script>

<style>

</style>